package com.unisound.tracelog.common.utils;

import org.apache.commons.collections4.MapUtils;

import java.util.HashMap;
import java.util.Map;

public class CommonUtils {

    public static Map<Integer, Object> argsMap(Object[] args) {
        if (args == null || args.length == 0) return null;
        Map<Integer, Object> map = new HashMap<>();

        for (int i = 0; i < args.length; i++) {
            Object arg = args[i];
            map.put(i, arg);
        }
        return map;
    }

    public static String jsonResult(Object result, Map<String, String> errorMessageMap) {
        if (result == null && MapUtils.isEmpty(errorMessageMap)) return null;

        Map<String, Object> map = new HashMap<>();
        if (result != null) {
            map.put("result", result);
        }

        if (errorMessageMap != null && !errorMessageMap.isEmpty()) {
            map.put("errorMessage", errorMessageMap);
        }

        return JsonUtils.toJson(map);
    }

}
